package com.mmw.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmw.crm.bo.CluePageBo;
import com.mmw.crm.entity.Clue;
import com.mmw.crm.result.Result;
import com.mmw.crm.service.ClueService;
import com.mmw.crm.vo.ClueSaveEntityVo;
import com.mmw.crm.vo.ClueSaveVo;
import com.mmw.crm.vo.CustomerVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
public class ClueController {
    @Resource
    ClueService clueService;

    @PostMapping("CrmLeads/queryPageList")
    public Result crmLeadsQueryPageList(@RequestBody CustomerVo customerVo){
        Integer page = customerVo.getPage();
        Integer limit = customerVo.getLimit();
        String search = customerVo.getSearch();

        Page<Clue> pageInfo = new Page<>(page,limit);
        LambdaQueryWrapper<Clue> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(search)){
            wrapper.like(Clue::getLeadsName,search).or().like(Clue::getTelephone, search)
                    .or().like(Clue::getMobile,search);
        }
        clueService.page(pageInfo,wrapper);

        List<Clue> records = pageInfo.getRecords();
        CluePageBo cluePageBoPageBo = new CluePageBo();
        cluePageBoPageBo.setTotalRow(records.size());
        cluePageBoPageBo.setList(records);
        return Result.build(cluePageBoPageBo,null,0);
    }
    @PostMapping("/CrmLeads/addOrUpdate")
    public Result addOrUpdate(@RequestBody ClueSaveVo clueSaveVo){

        ClueSaveEntityVo entity = clueSaveVo.getEntity();
        System.err.println(entity);
        Clue clue = new Clue();
        clue.setLeadsName(entity.getLeads_name());
        clue.setMobile(entity.getMobile());
        clue.setAddress(entity.getAddress());
        clue.setNextTime(entity.getNext_time());
        clue.setRemark(entity.getRemark());
        clue.setTelephone(entity.getTelephone());
        clue.setCreateUserId(1L);
        clue.setCreateTime(new Date());
        clue.setBatchId("1");
        System.err.println(clue);
        clueService.save(clue);

        return Result.build(null,null,0);
    }

    @PostMapping("/CrmLeads/deleteByIds")
    public Result crmLeadsDeleteByIds(@RequestParam List<Integer> leadsIds){
        System.err.println(leadsIds.toString());

            clueService.removeByIds(leadsIds);
            return Result.build(null,null,0);

    }

}
